package com.gbq.qimo.service.impl;

import com.gbq.qimo.mapper.StudentMapper;
import com.gbq.qimo.mapper.impl.StudentMapperImpl;
import com.gbq.qimo.pojo.PageBean;
import com.gbq.qimo.pojo.Student;
import com.gbq.qimo.service.StudentService;

import java.util.List;
import java.util.Map;

/**
 * @ClassName StudentServiceImpl
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/5/29 15:57
 */
public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper = new StudentMapperImpl();

    @Override
    public Student findStudent(Student student) {
//        System.out.println(student);
        return studentMapper.findOneStudent(student.getUsername(),student.getPassword());
    }

    @Override
    public List<Student> findAllStudents() {
        return studentMapper.findAllStudents();
    }

    @Override
    public int addStudent(Student student) {
        return studentMapper.addOneStudent(student);
    }

    @Override
    public Student findStudentById(Integer id) {
        return studentMapper.findStudentById(id);
    }

    @Override
    public Integer updateStudent(Student student) {
        return studentMapper.updateStudent(student);
    }

    @Override
    public int deleteStudentById(int id) {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public PageBean<Student> findBookByPage(String currentPage, String rows, Map<String, String[]> condition) {
        int currentPa = Integer.parseInt(currentPage);
        int row = Integer.parseInt(rows);
        //1.创建空的PageBean对象
        PageBean<Student> pb = new PageBean<>();
        //2.设置参数
        pb.setCurrentPage(currentPa);
        pb.setRows(row);
        //3.调用dao查询总记录数
        int totalCount = studentMapper.findTotalCount(condition);
        pb.setTotalCount(totalCount);
        int start = (currentPa - 1) * row;
        List<Student> list = studentMapper.findByPage(start,row,condition);
        pb.setList(list);
        //5.计算总页码
        int totalPage = (totalCount % row)  == 0 ? totalCount/row : (totalCount/row) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }

    @Override
    public int delSelectedStudents(String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            int id = Integer.parseInt(ids[i]);
            studentMapper.deleteStudentById(id);
        }

        return ids.length;
    }

    @Override
    public Student findStudentByUsername(Student student) {
        return studentMapper.findByUsername(student.getUsername());
    }

    @Override
    public int register(Student loginStudent) {
        return studentMapper.register(loginStudent.getUsername(),loginStudent.getPassword());
    }
}
